//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var no=thisform.find('input[name=no]').val();
		var name=thisform.find('input[name=name]').val();
		var erp_name=thisform.find('input[name=erp_name]').val();
		$('#contactsCompanyDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#contactsCompanyDatagrid').datagrid({ 
			queryParams:{
				no:no,
				name:name
			}
		});
	})
	
	//更新行数据
	function contactsCompanyUpdateActions(value){
		$.post(contactsCompanyEditUrl,{id:value.id,name:value.name,no:value.no},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#contactsCompanyDatagrid').datagrid('validateRow', editIndex)){
			$('#contactsCompanyDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#contactsCompanyDatagrid').datagrid('selectRow', index);
				$('#contactsCompanyDatagrid').datagrid('endEdit', editIndex);
				$('#contactsCompanyDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#contactsCompanyDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#contactsCompanyDatagrid').datagrid({
		//url:getcontactsCompanyUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getcontactsCompanyUrl,
		title:'往来单位列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'no',title:'公司编号',sortable:true},
		{field:'name',title:'公司名称',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'update_time',title:'更新时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					contactsCompanyUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
contactsCompanyObj={
	add:function(url){//新增数据
		$('#contactsCompanyAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#contactsCompanyAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#contactsCompanyAddDialog').dialog('close');       
						$('#contactsCompanyDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	save:function(url){//编辑后保存数据
		$('#contactsCompanyEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#contactsCompanyEditDialog').dialog('close');
                    });
                }else{
                    $('#contactsCompanyEditDialog').dialog('close');      
                    $('#contactsCompanyDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#contactsCompanyDatagrid').datagrid('validateRow', editIndex)){
			$('#contactsCompanyDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#contactsCompanyDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#contactsCompanyDatagrid').datagrid({ queryParams:''});
	},
	/* infoBar:function(){//显示详情
		var contactsCompanyRow=$('#contactsCompanyDatagrid').datagrid('getSelections');
        if (contactsCompanyRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(contactsCompanyRow.length==1){
			var id=contactsCompanyRow[0]['id'];
        	showTab(contactsCompanyInfoUrl,'账册详情',id);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}, */
	reloadBar:function(){//刷新数据
		$('#contactsCompanyDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#contactsCompanyDatagrid').datagrid({ queryParams:{status:-1}});
	},
	addBar:function(){//显示添加框
		$('#contactsCompanyAddDialog').dialog('open').dialog('setTitle','添加');
		$('#contactsCompanyAddForm').form('clear');
	},
	editBar:function(){//显示编辑框
		var contactsCompanyRow=$('#contactsCompanyDatagrid').datagrid('getSelections');
        if (contactsCompanyRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(contactsCompanyRow.length==1){            	
        	$('#contactsCompanyEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#contactsCompanyEditForm').form('load',contactsCompanyRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var contactsCompanyRow=$('#contactsCompanyDatagrid').datagrid('getSelections');
        if (contactsCompanyRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+contactsCompanyRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<contactsCompanyRow.length;i++){
                		ids.push(contactsCompanyRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(contactsCompanyDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#contactsCompanyAddDialog').dialog('close');       
								$('#contactsCompanyDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var contactsCompanyRow=$('#contactsCompanyDatagrid').datagrid('getSelections');
        if (contactsCompanyRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+contactsCompanyRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<contactsCompanyRow.length;i++){
                		ids.push(contactsCompanyRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(contactsCompanyClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#contactsCompanyAddDialog').dialog('close');       
								$('#contactsCompanyDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var contactsCompanyRow=$('#contactsCompanyDatagrid').datagrid('getSelections');
        if (contactsCompanyRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+contactsCompanyRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<contactsCompanyRow.length;i++){
                		ids.push(contactsCompanyRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(contactsCompanyUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#contactsCompanyAddDialog').dialog('close');       
								$('#contactsCompanyDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	examineBar:function(value){//审核数据
		var contactsCompanyRow=$('#contactsCompanyDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (contactsCompanyRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<contactsCompanyRow.length;i++){
                		ids.push(contactsCompanyRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(contactsCompanyExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#contactsCompanyDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}